package com.cowain.controller;

import com.cowain.api.code.SystemCode;
import com.cowain.api.commons.ResponseResult;
import com.cowain.api.commons.ResponseResultFactory;
import com.cowain.api.commons.SystemUtils;
import com.cowain.api.constant.Constant;
import com.cowain.entity.UserEntity;
import com.cowain.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @Author fxw
 * @Date 2022/5/12 20:44
 */
@RestController
@RequestMapping("/user")
public class UserController {
    final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private UserService service;

    @RequestMapping(value = "/addUser", method = RequestMethod.GET)
    public ResponseResult addUser(UserEntity userEntity) {
        if (SystemUtils.isNULL(userEntity)) {
            logger.error("---------------->System UserEntity Add Fail");
            return ResponseResultFactory.buildResponseResult(SystemCode.TRAFFIC_USER_ERROR_PARAM_NULL, "实体为空");
        }
        return checkParamEmpty(userEntity);
    }

    public ResponseResult checkParamEmpty(UserEntity userEntity) {
        if (SystemUtils.isNULLEmpty(userEntity.getUname())) {
            logger.error("---------------->System Username is null");
            return ResponseResultFactory.buildResponseResult(SystemCode.TRAFFIC_USER_ERROR_NAME_NULL, "用户名为空");
        }
        if (SystemUtils.isNULLEmpty(userEntity.getUccount())) {
            logger.error("---------------->System userCount is null");
            return ResponseResultFactory.buildResponseResult(SystemCode.TRAFFIC_USER_ERROR_ACCOUNT_NULL, "账户为空");
        }
        if (SystemUtils.isNULLEmpty(userEntity.getUpass())) {
            logger.error("---------------->System userPass is null");
            return ResponseResultFactory.buildResponseResult(SystemCode.TRAFFIC_USER_ERROR_PASS_NULL, "密码为空");
        }
        if (SystemUtils.isNULLEmpty(userEntity.getUphone())) {
            logger.error("---------------->System UserPhone is null");
            return ResponseResultFactory.buildResponseResult(SystemCode.TRAFFIC_USER_ERROR_PHONE_NULL, "手机号为空");
        }
        if (userEntity.getUname().length() < Constant.NAME_LENGTH) {
            logger.error("---------------->System UserName length < 3");
            return ResponseResultFactory.buildResponseResult(SystemCode.TRAFFIC_USER_ERROR_NAME_SIZE, "用户名长度小于三");
        }
        if (userEntity.getUccount().length() < Constant.ACCOUNT_LENGTH) {
            logger.error("---------------->System UserAccount length < 6");
            return ResponseResultFactory.buildResponseResult(SystemCode.TRAFFIC_USER_ERROR_ACCOUNT_SIZE, "账户长度小于6");
        }
        if (userEntity.getUpass().length() < Constant.PASSWORD_LENGTH) {
            logger.error("---------------->System UserPassword length < 6");
            return ResponseResultFactory.buildResponseResult(SystemCode.TRAFFIC_USER_ERROR_PASSWORD_SIZE, "密码长度小于6");
        }
        //密码加密存储
        userEntity.setUpass(DigestUtils.md5DigestAsHex(userEntity.getUpass().getBytes()));
        if (service.addUser(userEntity)) {
            return ResponseResultFactory.buildResponseResult();
        }
        logger.info("---------------->System User Add Successful");
        return ResponseResultFactory.buildResponseResult(SystemCode.SYSTEM_USER_ERROR_ADD_FAIL);
    }

    /**
     * 删除用户
     *
     * @param uid
     * @return
     */
    @RequestMapping(value = "/delUser", method = RequestMethod.GET)
    public ResponseResult delUser(String uid) {
        if (SystemUtils.isNULLEmpty(uid)) {
            return ResponseResultFactory.buildResponseResult(SystemCode.TRAFFIC_USER_ERROR_DEL_FAIL_UID_NULL, "Id为空");
        }

        logger.info("System User Del User Uid start");
        boolean result = service.delUser(uid);
        logger.info("System User Del User Uid end");
        return getResult(result);
    }

    /**
     * 更新用户信息
     *
     * @param userEntity
     * @return
     */
    @RequestMapping(value = "/UpdateUser", method = RequestMethod.GET)
    public ResponseResult updateUser(UserEntity userEntity) {
        logger.info("System User update User Uid start");
        if (SystemUtils.isNULL(userEntity)) {
            logger.error("---------------->System UserEntity Add Fail");
            logger.info("System UserEntity Return ---------->msg " + ResponseResultFactory.buildResponseResult(SystemCode.TRAFFIC_USER_ERROR_UPDATE_FAIL_PARAM_NULL, "实体为空"));
            return ResponseResultFactory.buildResponseResult(SystemCode.TRAFFIC_USER_ERROR_UPDATE_FAIL_PARAM_NULL, "实体为空");
        }
        if (SystemUtils.isNULL(userEntity.getUid()) || userEntity.getUid() == Constant.ZERO) {
            logger.error("---------------->System UserEntity Add Fail");
            logger.info("System UserEntity Return ---------->msg " + ResponseResultFactory.buildResponseResult(SystemCode.TRAFFIC_USER_ERROR_UPDATE_FAIL_UID_NULL, "实体为空"));
            return ResponseResultFactory.buildResponseResult(SystemCode.TRAFFIC_USER_ERROR_UPDATE_FAIL_UID_NULL, "iD为空");
        }
        boolean rel = service.updateUser(userEntity);
        logger.info("System User update User Uid end");
        return getResult(rel);
    }

    @RequestMapping(value = "/allUser")
    public ResponseResult<List<UserEntity>> findAll(UserEntity userEntity) {
        List<UserEntity> list = service.findAll();
        return ResponseResultFactory.buildResponseResult(SystemCode.TRAFFIC_SYSTEM_SUCCESS, list);
    }
    @RequestMapping(value = "/findAllByWhere")
    public ResponseResult<List<UserEntity>> findAllByWhere(UserEntity userEntity){
        List<UserEntity> allByWhere = service.findAllByWhere(userEntity);
        return ResponseResultFactory.buildResponseResult(SystemCode.TRAFFIC_SYSTEM_SUCCESS, allByWhere);
    }
    private ResponseResult getResult(boolean result) {
        if (result) {
            logger.info("System User Update User Uid successful");
            return ResponseResultFactory.buildResponseResult(SystemCode.TRAFFIC_SYSTEM_SUCCESS, "更新成功");
        } else {
            logger.info("System User Update User Uid fail");
            return ResponseResultFactory.buildResponseResult(SystemCode.TRAFFIC_SYSTEM_ERROR, "更新失败");
        }
    }
    @RequestMapping(value = "/findUserByTime")
    public ResponseResult<List<UserEntity>> findUserByTime(UserEntity userEntity){
        List<UserEntity> allByWhere = service.findUserByTime(userEntity.getStartTime().toString(),userEntity.getEndTime().toString());
        return ResponseResultFactory.buildResponseResult(SystemCode.TRAFFIC_SYSTEM_SUCCESS, allByWhere);
    }


    /**
     * 通用的分页查询
     * 排序,分页,条件查询
     * @param userEntity
     * @return
     */
    @RequestMapping("/queryUsers")
    public ResponseResult queryUsers(UserEntity userEntity){

        //分页查询+条件查询
        Map<String, Object> map = service.queryUsers(userEntity);
        ResponseResult responseResult = ResponseResultFactory.buildResponseResult(SystemCode.TRAFFIC_SYSTEM_SUCCESS,map);

        return responseResult;
    }
}
